package com.prs.crm.action.hr.report;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.ReportAction;
import com.prs.crm.context.Constrains;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.report.ChartResult;
import com.prs.crm.report.ChartService;
import com.prs.crm.report.ExportConfig;
import com.prs.crm.report.ReportCell;
import com.prs.crm.report.ReportTable;
import com.prs.crm.report.ReportUtils;
import com.prs.crm.service.hr.PersonService;

public class PersonJobTitleReportAction extends ReportAction{

	private static final long serialVersionUID = 6285933838433308223L;
	private ChartResult chartResult;
	private ChartService chartService;
	private PersonService personService;
	private PaginationSupport pagination;
	private Person person;
	private String category;
	private int exportType;
	
	
	@Action("editPersonJobTitle")
	public String editPersonJobTitle(){
		return SUCCESS;
	}
	@Action(value = "viewPersonJobTitlePie", results = { @Result(name = "success", type = "chain", location = "editPersonJobTitle") })
	public String viewPersonJobTitlePie(){
		Collection<?> dataList = this.getPersonService().getReportPersonJobTitleSource();
		chartResult = this.getChartService().export(this.getPersonService().fillDataset(dataList, Constrains.REPORT_PIECHART), "人员职称统计分析", "listReportPersonJobTitle");
		return SUCCESS;
	}
	@Action(value = "viewPersonJobTitleBar", results = { @Result(name = "success", type = "chain", location = "editPersonJobTitle") })
	public String viewPersonJobTitleBar(){
		Collection<?> dataList = this.getPersonService().getReportPersonJobTitleSource();
		chartResult = this.getChartService().export(this.getPersonService().fillDataset(dataList, Constrains.REPORT_BARCHART), "人员职称统计分析", "数量", "", "listReportPersonJobTitle");
		return SUCCESS;
	}
	
	@Action("listReportPersonJobTitle")
	public String listReportPersonJobTitle(){
		category = ReportUtils.URLDecode(this.getCategory());
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Person.class);
		criteria.createAlias("resume", "rs");
		criteria.createAlias("rs.title", "t");
		criteria.add(Restrictions.eq("t.name", category));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPersonService().listByPage(criteria,
				pagination));
		return SUCCESS;
	}
	@Action(value = "exportPersonJobTitleReport", results = { @Result(name = "success", type = "stream", params = {
			"contentType", "${contentType}", "inputName", "inputStream",
			"bufferSize", "1024", "contentDisposition",
			"filename=${outfileName}" }) })
	public String exportPersonJobTitleReport(){
		ReportTable table = new ReportTable("人员职称统计分析报表");
		table.addCell(new ReportCell("序号","id"));
		table.addCell(new ReportCell("人员编码","code"));
		table.addCell(new ReportCell("登录名","loginCode"));
		table.addCell(new ReportCell("真实姓名","resume.name"));
		table.addCell(new ReportCell("部门","dept.name"));
		table.addCell(new ReportCell("职位","role.name"));
		table.addCell(new ReportCell("职称","resume.title.name"));
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Person.class);
		criteria.createAlias("resume", "rs");
		criteria.createAlias("rs.title", "t");
		criteria.add(Restrictions.eq("t.name", this.getCategory()));
		this.setOutFilePath(this.getReportService().report(criteria, table,
				new ExportConfig(), this.getExportType()));
		return SUCCESS;
	}
	
	
	
	public ChartResult getChartResult() {
		return chartResult;
	}
	public void setChartResult(ChartResult chartResult) {
		this.chartResult = chartResult;
	}
	public ChartService getChartService() {
		return chartService;
	}
	public void setChartService(ChartService chartService) {
		this.chartService = chartService;
	}
	public PersonService getPersonService() {
		return personService;
	}
	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Person getPerson() {
		return person;
	}
	public void setPerson(Person person) {
		this.person = person;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public int getExportType() {
		return exportType;
	}
	public void setExportType(int exportType) {
		this.exportType = exportType;
	}
	
	
}
